﻿/*
Copyright 2013 Google Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.Collections.Generic;

namespace Google.Apis.Auth.OAuth2.Responses
{
    /// <summary>
    /// Authorization Code response for the redirect URL after end user grants or denies authorization as specified 
    /// in http://tools.ietf.org/html/rfc6749#section-4.1.2.
    /// <para>
    /// Check that <see cref="Code"/> is not <c>null</c> or empty to verify the end-user granted authorization.
    /// </para>
    /// </summary>
    public class AuthorizationCodeResponseUrl
    {
        /// <summary>Gets or sets the authorization code generated by the authorization server.</summary>
        public string Code { get; set; }

        /// <summary>
        /// Gets or sets the state parameter matching the state parameter in the authorization request.
        /// </summary>
        public string State { get; set; }

        /// <summary>
        /// Gets or sets the error code (e.g. "invalid_request", "unauthorized_client", "access_denied", 
        /// "unsupported_response_type", "invalid_scope", "server_error", "temporarily_unavailable") as specified in 
        /// http://tools.ietf.org/html/rfc6749#section-4.1.2.1.
        /// </summary>
        public string Error { get; set; }

        /// <summary>
        /// Gets or sets the human-readable text which provides additional information used to assist the client 
        /// developer in understanding the error occurred.
        /// </summary>
        public string ErrorDescription { get; set; }

        /// <summary>
        /// Gets or sets the URI identifying a human-readable web page with provides information about the error.
        /// </summary>
        public string ErrorUri { get; set; }

        /// <summary>Constructs a new authorization code response URL from the specified dictionary.</summary>
        public AuthorizationCodeResponseUrl(IDictionary<string, string> queryString)
        {
            InitFromDictionary(queryString);
        }

        #region Constructs

        /// <summary>Constructs a new authorization code response URL from the specified query string.</summary>
        public AuthorizationCodeResponseUrl(string query)
        {
            var pairs = query.Split('&');
            var queryString = new Dictionary<string, string>();
            foreach (var pair in pairs)
            {
                var keyValue = pair.Split('=');
                queryString[keyValue[0]] = keyValue[1];
            }

            InitFromDictionary(queryString);
        }

        /// <summary>Initializes this instance from the input dictionary.</summary>
        private void InitFromDictionary(IDictionary<string, string> queryString)
        {
            //TODO(peleyal): improve the following code and make it a utility
            IDictionary<string, Action<string>> setters = new Dictionary<string, Action<string>>();
            setters["code"] = v => Code = v;
            setters["state"] = v => State = v;
            setters["error"] = v => Error = v;
            setters["error_description"] = v => ErrorDescription = v;
            setters["error_uri"] = v => ErrorUri = v;

            Action<string> setter;
            foreach (var pair in queryString)
            {
                if (setters.TryGetValue(pair.Key, out setter))
                {
                    setter(pair.Value);
                }
            }
        }

        /// <summary>Constructs a new empty authorization code response URL.</summary>
        public AuthorizationCodeResponseUrl()
        {
        }

        #endregion
    }
}
